class bullet {
  constructor(board, player) {
    this.id = board.bulletsId
    this.ctx = board.ctx
    this.x = player.x
    this.y = player.y + 30
    this.positionX = null
    this.width = 10
    this.height = 10
    this.speed = 0
    this.bullets = board.bullets
    this.timer = null
    this.frame()
  }
  // 创建子弹
  draw() {
    this.speed += 20
    this.positionX = this.x + 50 + this.speed
    this.ctx.fillStyle = "rgb(255,255,255)"
    this.ctx.fillRect(this.positionX, this.y, this.width, this.height)
    if (this.positionX > 950) {
      this.delete()
    }
  }
  delete() {
    delete this.bullets[this.id]
    cancelAnimationFrame(this.timer)
  }
  suspend(){
    cancelAnimationFrame(this.timer)
  }
  frame() {
    this.timer = requestAnimationFrame(() => {
      this.frame()
    })
    this.draw()
  }
}

export { bullet }